﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TCMS.Models.Entities;
using System.Web.Script.Serialization;
using TCMS.Models.DAO;
using TCMS.Models;
using System.IO;
using System.Text.RegularExpressions;

namespace tcms.Controllers
{
    [Authorize]
    public class ShopFloorController : Controller
    {
        private ShopFloorDAO _shopFloorDAO = DAOFactory.getShopFloorDAO();
        private TaskDAO _taskDAO = DAOFactory.getTaskDAO();

        //
        // GET: /ShopFloor/

        [Authorize(Roles = "admin, manager")]
        public ActionResult Index()
        {
            List<ShopFloorEntity> shopFloors = _shopFloorDAO.getShopFloors();
            ViewBag.ShopFloors = shopFloors;

            foreach (var sf in shopFloors)
            {
                sf.Task = _taskDAO.getSingleTaskByShop(sf.ShopName);
            }
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            ViewBag.SerializedShopFloors = serializer.Serialize(shopFloors);

            var path = Server.MapPath(Url.Content("~/Content/map/"));

            var files = Directory.GetFiles(path, "*.png");
            Array.Sort(files);
            ViewBag.Icons = files.Select(o => new FileInfo(o).Name).Where(f => Regex.IsMatch(f, @"ICON_\d{2}\.png"));

            ViewBag.IsInEditMode = HttpContext.User.IsInRole("admin");

            if (HttpContext.User.IsInRole("manager"))
            {
                ViewBag.Departments = _taskDAO.GetDepartments();
                ViewBag.Teams = _taskDAO.GetTeams();
            }

            return View();
        }

        public ActionResult Status(string id)
        {
            ViewBag.ShopName = id;
            TaskEntity task = _taskDAO.getSingleTaskByShop(id);

            return View(task);
        }

        [HttpPost]
        [Authorize(Roles = "admin")]
        public string UpdatePositions(string data)
        {
            try
            {
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                var shopFloors = serializer.Deserialize<ShopFloorEntity[]>(data);

                //foreach (var item in shopFloors)
                //{
                //    _shopFloorDAO.addNewShopFloor(item);
                //}
                _shopFloorDAO.UpdateShopFloors(shopFloors);
            }
            catch (Exception ex)
            {
                Response.StatusCode = 500;
                return ex.Message;
            }

            return "保存成功";
        }

        [HttpPost]
        [Authorize(Roles = "admin")]
        public string DeleteShopFloor(Guid id)
        {
            try
            {
                _shopFloorDAO.DeleteShopFloor(id);
            }
            catch (Exception ex)
            {
                Response.StatusCode = 500;
                return ex.Message;
            }

            return "删除成功";
        }

        [HttpPost]
        public ActionResult Search(string department, string team, string shopFloor, DateTime? from, DateTime? to)
        {
            ViewBag.Departments = _taskDAO.GetDepartments();
            ViewBag.Teams = _taskDAO.GetTeams();
            ViewBag.ShopFloors = _shopFloorDAO.getShopFloors();

            ViewBag.Department = department;
            ViewBag.Team = team;
            ViewBag.ShopName = shopFloor;
            ViewBag.From = (from == null) ? "" : from.Value.ToString("yyyy-MM-dd");
            ViewBag.To = (to == null) ? "" : to.Value.ToString("yyyy-MM-dd");

            return View();
        }

        [HttpGet]
        public JsonResult Search(int? page, int? pagesize, string department, string team, string shopFloor, DateTime? from, DateTime? to)
        {
            var tasks = _taskDAO.Search(page, pagesize, department, team, shopFloor, from, to);

            var data = new { Total = tasks.Count, Rows = tasks };

            return Json(data, JsonRequestBehavior.AllowGet);
        }
    }
}
